package com.turtle.security.organizates.controller;

import com.turtle.security.common.annotation.OperateLog;
import com.turtle.security.common.utils.ApiResult;
import com.turtle.security.organizates.dto.SysDeptAddDTO;
import com.turtle.security.organizates.dto.SysDeptUpdateDTO;
import com.turtle.security.organizates.service.impl.SysDeptServiceImpl;
import com.turtle.security.system.enums.OperateClient;
import com.turtle.security.system.enums.OperateType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统部门表
 *
 * @author turtle
 * date： 2020-06-04
 */
@RestController
@Api(value = "系统部门模块", tags = "系统部门模块")
@RequestMapping("/organizates/dept")
public class SysDeptController {

    @Autowired
    private SysDeptServiceImpl sysDeptService;

    /**
     * 添加部门
     *
     * @param sysDept 部门信息
     * @return ApiResult
     */
    @ApiOperation(value = "添加部门")
    @PostMapping("add")
    @OperateLog(operateClient = OperateClient.MANAGE, module = "部门模块", operateType = OperateType.INSERT, description = "新增部门")
    public ApiResult deptAdd(@Validated @RequestBody SysDeptAddDTO sysDept) {
        sysDeptService.isExistParentDept(sysDept.getParentId());
        sysDeptService.isExistDeptName(sysDept.getDeptName(), 0L);
        sysDeptService.deptAdd(sysDept);
        return ApiResult.success("添加部门成功");
    }

    /**
     * 更新部门
     *
     * @param sysDept 部门信息
     * @return ApiResult
     */
    @ApiOperation(value = "更新部门")
    @PutMapping("update")
    @OperateLog(operateClient = OperateClient.MANAGE, module = "部门模块", operateType = OperateType.UPDATE, description = "更新部门")
    public ApiResult deptUpdate(@Validated @RequestBody SysDeptUpdateDTO sysDept) {
        sysDeptService.isExistDept(sysDept.getDeptId());
        sysDeptService.isExistParentDept(sysDept.getParentId());
        sysDeptService.isExistDeptName(sysDept.getDeptName(), sysDept.getDeptId());
        sysDeptService.deptUpdate(sysDept);

        return ApiResult.success("更新部门成功");
    }

    /**
     * 删除部门
     *
     * @param deptId 部门id
     * @return ApiResult
     */
    @ApiOperation(value = "删除部门")
    @DeleteMapping("delete/{deptId}")
    @OperateLog(operateClient = OperateClient.MANAGE, module = "部门模块", operateType = OperateType.DELETE, description = "删除部门")
    public ApiResult deptDel(@Validated @PathVariable Long deptId) {
        sysDeptService.isExistDept(deptId);
        sysDeptService.isExistChildren(deptId);
        sysDeptService.deptDel(deptId);

        return ApiResult.success("部门删除成功");
    }

}